<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;


class CtypeController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $list = \DB::select("select * from classtwo order by concat(pidstr,id) asc");
        
        //处理信息
        foreach($list as &$v){
            $m = substr_count($v->pidstr,","); //获取path中的逗号
            //生成缩进
            $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
        }
        
        return view("admin.ctype.index",['list'=>$list]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
		$list = \DB::select("select * from classtwo order by concat(pidstr,id) asc");
        
        //处理信息
        foreach($list as &$v){
            $m = substr_count($v->pidstr,","); //获取pidstr中的逗号
            //生成缩进
            $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
        }
		
		return view("admin.ctype.create",['list'=>$list]);
       
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
		
        //获取要添加的数据
        $data = $request->only("name",'pid');
        $pid = $data['pid'];
        if($pid==0){
            $data['pidstr']="0,";
        }else{
            $type = \DB::table("classtwo")->where("id",$pid)->first();
            $data['pidstr'] = $type->pidstr.$pid.",";
			
        }
        
        //执行添加
        $id = \DB::table("classtwo")->insertGetId($data);
        //判断
        if($id>0){
            $info = "类别信息添加成功！";
        }else{
            $info = "类别信息添加失败！";
        }
        
        return redirect("admin/ctype")->with("err",$info);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
      
       $list = \DB::table("classtwo")->where("id",$id)->first();
		
		return view("admin.ctype.edit",['list'=>$list]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
         $data = $_POST;
		unset($data['_token']);
		unset($data['_method']);
		
		$id = \DB::table("classtwo")->where("id",$id)->update($data);
		
		//判断
        if($id>0){
            $info = "类别信息修改成功！";
        }else{
            $info = "类别信息修改失败！";
        }
        
       return redirect("admin/ctype")->with("err",$info);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //先判断当前类别下是否存在子类别
        $m = \DB::table('classtwo')->where('pid',$id)->count();
        if($m>0){
            return back()->with("err","禁止删除");
        }  
      
        \DB::table('classtwo')->delete($id);
        return redirect("admin/ctype")->with("err","删除成功！");
    }
}
